<?php
/**
 * 定义用户的注册行为
 *
 *
 */
class user_reg_process{

	private $db_connect;	
	private $db_obj_user;
	private $db_obj_user_profile;
	
	function __construct( $db_connect ){

		//用来存放用户的属性
		$this->db_obj_user = new db_obj_user( $db_connect );
		$this->db_obj_user_profile = new db_obj_user_profile( $db_connect );	
	}

	//判断该用户名是否已经被注册	
	function has_reg( $email ){

		return $this->db_obj_user->load( 'email' , $email );
	}

	function do_reg( $request ){
	 	 
		$email = $request['email'];
		$password = $request['password'];
		$nick = $request['nick'];

		if( $this->has_reg( $email )){
	 	 	 
			 throw new Exception( '该邮箱已经被注册.' );
		}

		$this->db_obj_user->set( 'email' , $email );
		$this->db_obj_user->set( 'password' , md5( $password ) );
		$this->db_obj_user->set( 'nick' , $nick );

		//设置profile表
		$this->db_obj_user_profile->set( 'email' , $email );
		//设置默认的头像
		$this->db_obj_user_profile->set( 'head_url' , '/classmate/upload/user_head_img/anonymous.jpg' );
		//设置用户注册的时间
	 	$this->db_obj_user_profile->set( 'reg_time' , date( 'Y-m-d' ));

		if ( $this->db_obj_user->save() && $this->db_obj_user_profile->save() ){
		
			 return true;
		}else{

			 //@todo 定义不同级别的异常,捕获时区分之,以采取不同的处理方式.
	 	 	 throw new Exception( '由于不可预料的原因注册失败.' );
		}

	}

}
?>
